<template>
	<div>
		<view class="wrap">
			<view class="wrap-list">
				<block v-for="(item, index) in list" :key="index">
					<view class="item" @click="onItem(item)">
						<image class="file" :src="getImageURL(item.cover)" lazy-load @error="onImageError(index)" />
			
						<view class="name-btn">
							<view class="name limit-num-line">
								{{ item.title }}
							</view>
			
							<view class="btn" v-if="$filters.isCanApply(item)"> 开始报名 </view>
							<view class="btn bbttnn" v-else> 已报名 </view>
						</view>
					</view>
				</block>
			</view>
		</view>
	</div>
</template>

<script>
	import { getCompetitionList } from "@/utils/api.js";
	export default {
		data() {
			return {
				page: 1,
				total: 0,
				list: [],
			}
		},
		onLoad() {
			this.onRefresh();
		},
		onPullDownRefresh() {
			this.onRefresh();
		},
		onReachBottom() {
			if (this.list.length < this.total) {
				this.page++;
				this.getData();
			}
		},
		methods: {
			/**
			 * 图片加载失败
			 */
			onImageError(index) {
				this.list[index].cover = "/static/common/default_icon.png";
			},
			/**
			 * 列表点击
			 */
			onItem(item) {
				uni.navigateTo({
					url: `/pages/registration/eventDetails?id=${item.competitionId}`,
				});
			},
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1;
				this.getData();
			},
			/**
			 * 网络请求
			 */
			getData() {
				getCompetitionList(this.page).then((res) => {
					this.total = res.data.total;
					const list = res.data.data;
					if (this.page === 1) {
						this.list = list;
					} else {
						this.list = this.list.concat(list);
					}
					let title = "创野报名";
					if (this.total > 1) {
						title = "赛事列表";
					}
					uni.setNavigationBarTitle({
						title: title,
					});
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f6f6f6;
	}
	.wrap {
		--limit-num: 1;
		width: 100%;
		overflow: hidden;
		background-color: white;
		
		.wrap-list {
			width: 100%;
			overflow: hidden;
			padding: 20rpx 30rpx;
			box-sizing: border-box;
			overflow: hidden;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			justify-content: space-between;
		
			.item {
				width: 335rpx;
				margin-bottom: 20rpx;
				background-color: white;
				display: flex;
				flex-direction: column;
				align-items: center;
				border-radius: 20rpx;
				overflow: hidden;
		
				&:first-child {
					width: 690rpx !important;
				}
				&:first-child .file {
					height: 460rpx !important;
				}
				&:first-child .name {
					margin-top: 0rpx !important;
					flex: 1;
					padding: none !important;
				}
				&:first-child .btn {
					margin-top: 0rpx !important;
					margin-left: 10rpx;
				}
		
				&:first-child .name-btn {
					margin-top: 27rpx;
					flex-direction: row;
					padding: 0 20rpx;
					box-sizing: border-box;
				}
		
				.name-btn {
					width: 100%;
					overflow: hidden;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-between;
				}
				.file {
					width: 100%;
					height: 223rpx;
					border-top-left-radius: 10rpx;
					border-top-right-radius: 10rpx;
					overflow: hidden;
				}
		
				.name {
					margin-top: 20rpx;
					color: #333333;
					font-size: 30rpx;
					font-weight: bold;
					padding: 0 28rpx;
				}
		
				.btn {
					margin-top: 20rpx;
					width: 194rpx;
					height: 64rpx;
					line-height: 64rpx;
					text-align: center;
					border-radius: 32rpx;
					overflow: hidden;
					background-color: #ea5520;
					color: white;
					font-size: 28rpx;
				}
			}
		}
	}
</style>